///**********************
/// VARIABLES GENERALES
///**********************
const cFilename="index.html"         //Nombre del fichero con la web que contiene el proyecto 
const cFPS=50;						//FPS del juego
const cPAGE_PRELOAD ="pageintro";	//Nombre de la pagina de precarga (intro)
const cTRANSITION = "none";			//Tipo de transcicion entre paginas (valores: 'none', 'fade', 'flip', 'slide', 'slideup','slidedown', 'pop')
const cWIDTH_DEFAULT = 480;			//Anchura de las paginas por defecto
const cHEIGHT_DEFAULT = 640;		//Altura de las paginas por defecto

//***********************
//Variables del proyecto
//***********************
//Variables niveles
var levelnow;		 				//Nivel actual
var levelmax; 						//Nivel maximo alcanzado
var tipo=0;
var personas=1;
var importe_total=0;
var precio_comida=0;
var precio_postre=0;
var precio_cafe=0;
var precio_licor=0;

var nombre_evento;
var persons = new Array(16);
var menus = new Array(16);
var desserts = new Array(16);
var cafes = new Array(16);
var licors = new Array(16);
var cuenta = new Array(16);

for (i=0; i<16; i++) {
	persons[i]=false;
	menus[i]=false;
	desserts[i]=false;
	cafes[i]=false;
	licors[i]=false;
	cuenta[i]="0";
}

var perso_con_comida;
var perso_con_postre;
var perso_con_cafe;
var perso_con_licor;

var saveEvent = new Array();

var da = new Date();
dy = da.getFullYear();
dm = da.getMonth() + 1 	;
dd = da.getDate();
var now = dd + "/" + dm + "/" + dy;

///****************************************************************************************************
/// CONFIGURACION: Relaciona una variable del fichero de configuraciones con una variable del proyecto
///****************************************************************************************************
function cConfigUser() {
	graphic = cConfig[1];
}
///****************************************************
/// LIBRERIA: Elementos que se cargaran en el Preloader
///****************************************************
function cLibraryLoad() {
	_titulo = cLoadImg("img/titulo.png");
	_perso = cLoadImg("img/perso.png");
	_dessert = cLoadImg("img/dessert.png");
	_cafe = cLoadImg("img/cafe.png");
	_licor = cLoadImg("img/licor.png");		
	_pay = cLoadImg("img/pay.png");	
}
//*******************************************************************************************************
// cMenuLoad: Creamos las paginas del proyecto (Podemos indicarle un tamaño personalizado a cada pagina)
// Ej: cAddPage(cIndex,"pagetest", width, height);
//*******************************************************************************************************
function cMenuLoad() {
	cAddPage(cIndex,"pagemenu");
	cAddPage(cIndex,"pagepremain");
	cAddPage(cIndex,"pagemain");
	cAddPage(cIndex,"pagelist");
	cAddPage(cIndex,"pageoptions");
	cAddPage(cIndex,"pagesave");
}
///************///
/// PRELOADER  ///
///************///
//************************************************************************
//  Instruccions previas a la carga de todos los elementos de la libreria
//************************************************************************
function cPreLoader(ctx,width,height) {
	cx=width/2; cy=height/2;
	cText(ctx,true, cGeneral[0], cx, cy, "fs" , ["swhite",null], 36, 100, [1,1,2,"#CCC"],[100,36]);
}	
///****************************************************
/// LOADER INTRO 
///****************************************************
function loader_pageintro(ctx, page, width, height, strings) {
	cMenuLoad();
	cRefreshmenu= cIndex[1];
	cGo(cRefreshmenu);
}
///****************************************************
/// LOADER MENU PRINCIPAL 
///****************************************************
function loader_pagemenu(ctx, page, width, height,strings) {
	cx=width/2; cy=height/2;
	cClear(ctx, 0,0, width, height, "black", true);
	cRect(ctx,true, cx,cy-10,width-10,height-30,"fs4",["s#333399","s#333399"],8,100,[2,2,2,"#000"]);
	cImg(ctx,true, _titulo,cx,50,100,2);
	//Eslogan
	cText(ctx,true,  cGeneral[8], cx, 110, "f" , ["swhite","swhite"], 22,100,[1,1,1,"#CCC"]);
	//Cuadro menu
	cRect(ctx,true, cx,cy-20,width-100,320,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);
	//Cuadro titulomenu
	cRect(ctx,true, cx,cy-140,360,60,"fs4",["sblack","s#333399"],8,60,[2,2,2,"#000"]);
	cText(ctx,true,  strings[3], cx, 180, "f" , ["swhite","swhite"], 42,80,[1,1,1,"#000"]);
	//Ayuda
	cRect(ctx,true, cx,520,width-50,80,"fs4",["sblack","s#333399"],8,60,[2,2,2,"#000"]);
	cText(ctx,true,  strings[4], cx, 500, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  strings[5], cx, 520, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  strings[6], cx, 540, "f" , ["swhite","swhite"], "18px sans-serif");
	
	//Menu
	cLink(ctx, page, strings, "newplay", [cx,255,290,50], 'cGo("pagepremain")', 'ml_new(ctx, opt)'); 
	cLink(ctx, page, strings, "resume",[cx,335,290,50], 'load_all()', 'ml_list(ctx, opt)');
	cLink(ctx, page, strings, "options", [cx,415,290,50], 'cGo("pageoptions")', 'ml_options(ctx, opt)');
	
	//Footer
	cRect(ctx,true,cx,590,width-20,40,"f",["sblack",null],8);
	cText(ctx,true,  cGeneral[2], 70, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  cGeneral[1], cx, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cLink(ctx, page, cGeneral, "mail", [width-70,590,100,20], 'cGo("mailto://oriol.faura@gmail.com", 1)', 'ml_mail(ctx, opt, ' + width + ')');

}

function ml_mail (ctx, opt, width) {
	cText(ctx,opt[0], cGeneral[4],width-70,590,"f",["swhite",null],"18px sans-serif"); 
	cText(ctx,opt[1], cGeneral[4],width-70,590,"f",["syellow",null],"18px sans-serif");	
}

function ml_new (ctx, opt) {
	cText(ctx,opt[0], strings[0],cx,255,"f",["swhite",null],34,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[0],cx,255,"fs",["swhite","sred"],36,100,[1,1,1,"#CCC"]);	
}

function ml_list (ctx, opt) {
	cText(ctx,opt[0], strings[1],cx,335,"f",["swhite",null],34,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[1],cx,335,"fs",["swhite","sred"],36,100,[1,1,1,"#CCC"]);	
}

function ml_options (ctx, opt) {
	cText(ctx,opt[0], strings[2],cx,415,"f",["swhite",null],34,80,[1,1,1,"#CCC"]);
	cText(ctx,opt[1], strings[2],cx,415,"fs",["swhite","sred"],36,100,[1,1,1,"#CCC"]);
}

///****************************************************
/// LOADER MENU PREMAIN 
///****************************************************
function loader_pagepremain(ctx, page, width, height,strings) {

	cx=width/2; cy=height/2;
	cClear(ctx, 0,0, width, height, "black", true);
	cRect(ctx,true, cx,cy-10,width-10,height-30,"fs4",["s#333399","s#333399"],8,100,[2,2,2,"#000"]);
	cImg(ctx,true, _titulo,cx,50,100,2);
	//Eslogan
	cText(ctx,true,  cGeneral[8], cx, 110, "f" , ["swhite","swhite"], 22,100,[1,1,1,"#CCC"]);
	//Cuadro menu
	cRect(ctx,true, cx,cy-10,width-100,340,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);
	//Cuadro titulomenu
	cRect(ctx,true, cx,cy-140,360,60,"fs4",["sblack","s#333399"],8,60,[2,2,2,"#000"]);
	cText(ctx,true,  strings[0], cx, 180, "f" , ["swhite","swhite"], 42,80,[1,1,1,"#000"]);

	//Menu event
	cText(ctx,true,  strings[3], cx-82, 250, "s" , ["swhite","swhite"], 26,100,[1,1,1,"#000"]);
	cText(ctx,true,  strings[4], cx-82, 310, "s" , ["swhite","swhite"], 26, 100, [1,1,1,"#000"]);
	cText(ctx,true,  strings[5], cx-82, 370, "s" , ["swhite","swhite"], 26, 100, [1,1,1,"#000"]);	
	cText(ctx,true,  strings[6], cx-82, 430, "s" , ["swhite","swhite"], 26, 100, [1,1,1,"#000"]);	
	cText(ctx,true,  now, cx+88, 310, "f" , ["syellow","swhite"], 26, 100, [1,1,1,"#000"]);	
	//Menu nav
	cLink(ctx, page, strings, "volver", [cx-100,515,150,50], 'cGo("pagemenu")', 'pl_back(ctx, opt)');
	cLink(ctx, page, strings, "calcular", [cx+100,515,150,50], 'calculate(ctx, opt)', 'pl_main(ctx, opt)');

	//Footer
	cRect(ctx,true,cx,590,width-20,40,"f",["sblack",null],8);
	cText(ctx,true,  cGeneral[2], 70, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  cGeneral[1], cx, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cLink(ctx, page, cGeneral, "mail", [width-70,590,100,20], 'cGo("mailto://oriol.faura@gmail.com", 1)', 'ml_mail(ctx, opt, ' + width + ')');
}

function calculate (ctx, opt) {
	nombre_evento = jQuery("#evento").val(); 
	importe_total= parseFloat(jQuery("#importe").val()); 
	
	var compro=true;
	
	if (importe_total<=0 || importe_total>99999) {compro=false; alert("Importe total incorrecto");}
	
	personas= parseInt(jQuery("#personas").val()); 
	if (personas<=0 || personas>16) {compro=false; alert("Valor comprendido entre 1 y 16");}
	
	if (compro) {
		precio_comida = importe_total;
		$("#impo").val(importe_total);
	
		for (i=0;i<personas;i++) {
			persons[i] =true;
			menus[i]=true;
		}
		cGo("pagemain");		
	}
	

}

function pl_back (ctx, opt) {
	cText(ctx,opt[0], strings[1],cx-100,515,"f",["swhite",null],32,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[1],cx-100,515,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}

function pl_main (ctx, opt) {
	cText(ctx,opt[0], strings[2],cx+100,515,"f",["swhite",null],32,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[2],cx+100,515,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}

///****************************************************
/// LOADER MENU MAIN 
///****************************************************
function loader_pagemain(ctx, page, width, height,strings) {
	cx=width/2; cy=height/2;
	cClear(ctx, 0,0, width, height, "black", true);
	cRect(ctx,true, cx,cy-10,width-10,height-30,"fs4",["s#333399","s#333399"],8,100,[2,2,2,"#000"]);
	
	//Titulo
	cText(ctx,true,  "TOTAL: " +importe_total + "€", 400, 35, "f" , ["sred","swhite"], 24, 100, [1,1,1,"#000"]);
	cText(ctx,true,  nombre_evento, cx-20, 35, "f" , ["swhite","swhite"], 24, 100, [1,1,1,"#000"]);	
	cText(ctx,true,  now, 70, 35, "f" , ["syellow","swhite"], 24, 100, [1,1,1,"#000"]);	
	
	//Cuadro menu
	cRect(ctx,true, cx,cy-45,width-40,440,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);
	cRect(ctx,true, 158,cy+209,270,50,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);

	//Rectangulos
	if (tipo==0) {cRect(ctx,true, 60,cy+210,55,34,"fs2",["sgrey","sred"],4,100,[1,1,1,"#000"]);} else {cRect(ctx,true, 60,cy+210,55,34,"f",["sgrey","sgrey"],4,60,[1,1,1,"#000"]);}
	if (tipo==1) {cRect(ctx,true, 125,cy+210,55,34,"fs2",["syellow","sred"],4,100,[1,1,1,"#000"]);} else {cRect(ctx,true, 125,cy+210,55,34,"f",["syellow","syellow"],4,60,[1,1,1,"#000"]);}
	if (tipo==2) {cRect(ctx,true, 190,cy+210,55,34,"fs2",["spurple","sred"],4,100,[1,1,1,"#000"]);} else {cRect(ctx,true, 190,cy+210,55,34,"f",["spurple","spurple"],4,60,[1,1,1,"#000"]);}
	if (tipo==3) {cRect(ctx,true, 255,cy+210,55,34,"fs2",["sgreen","sred"],4,100,[1,1,1,"#000"]);} else {cRect(ctx,true, 255,cy+210,55,34,"f",["sgreen","sgreen"],4,60,[1,1,1,"#000"]);}
	
	//Iconos
	cImg(ctx,true,_perso,55,cy+210,100,[1,1,1,"#CCC"]);
	cRect(ctx,true, 80,cy+200,10,10,"f",["syellow","sred"],0,100);
	cRect(ctx,true, 80,cy+210,10,10,"f",["spurple","sred"],0,100);
	cRect(ctx,true, 80,cy+220,10,10,"f",["sgreen","sred"],0,100);
	
	cImg(ctx,true,_dessert,125,cy+210,100,[1,1,1,"#CCC"]);
	cImg(ctx,true,_cafe,190,cy+210,100,[1,1,1,"#CCC"]);
	cImg(ctx,true,_licor,255,cy+210,100,[1,1,1,"#CCC"]);
	
	//Personal
	Recalcule(ctx);
	mirar_tipo(ctx);
	//Menu
	cLink(ctx, page, strings, "volver2",[395,530,120,40], 'reset();', 'al_list(ctx, opt)');	
	cLink(ctx, page, strings, "guardar2", [395,585,120,40], 'save_all();', 'al_save(ctx, opt)'); 
	
	cLink(ctx, page, strings, "perso", [60,cy+210,55,34], 'perso_click(ctx, opt)', 'perso(ctx, opt)');
	cLink(ctx, page, strings, "dessert", [125,cy+210,55,34], 'dessert_click(ctx, opt)', 'dessert(ctx, opt)');
	cLink(ctx, page, strings, "cafe", [190,cy+210,55,34], 'cafe_click(ctx, opt)', 'cafe(ctx, opt)');
	cLink(ctx, page, strings, "licor", [255,cy+210,55,34], 'licor_click(ctx, opt)', 'licor(ctx, opt)');
}

function save_all() {
	saveEvent= new Array(); 
	saveEvent[0] = importe_total;
	saveEvent[1] = personas;	
	saveEvent[2] = jQuery("#evento").val(); 	
	saveEvent[3] = now;	
	saveEvent[4] = precio_comida;
	saveEvent[5] = precio_postre;
	saveEvent[6] = precio_cafe;
	saveEvent[7] = precio_licor;

	localStorage.setItem('saveEvent', JSON.stringify(saveEvent));
	localStorage.setItem('persons', JSON.stringify(persons));
	localStorage.setItem('menus', JSON.stringify(menus));
	localStorage.setItem('desserts', JSON.stringify(desserts));
	localStorage.setItem('cafes', JSON.stringify(cafes));
	localStorage.setItem('licors', JSON.stringify(licors));
	localStorage.setItem('cuenta', JSON.stringify(cuenta));
	cGo("pagesave");
}

function load_all() {
	saveEvent = JSON.parse(localStorage.getItem("saveEvent"));
	persons = JSON.parse(localStorage.getItem("persons"));
	menus = JSON.parse(localStorage.getItem("menus"));
	desserts = JSON.parse(localStorage.getItem("desserts"));
	cafes = JSON.parse(localStorage.getItem("cafes"));
	licors = JSON.parse(localStorage.getItem("licors"));
	cuenta = JSON.parse(localStorage.getItem("cuenta"));
 
	importe_total =saveEvent[0];
	personas =	saveEvent[1];
	$("#evento").val(saveEvent[2]);
	now= saveEvent[3];
	precio_comida= saveEvent[4]; 
	precio_postre= saveEvent[5]; 
	precio_cafe= saveEvent[6]; 
	precio_licor= saveEvent[7]; 

	cGo("pagemain");
}

function reset() {
 tipo=0;
 personas=1;
 importe_total=0;
 precio_comida=0;
 precio_postre=0;
 precio_cafe=0;
 precio_licor=0;

  persons = new Array();
  menus = new Array();
  desserts = new Array();
  cafes = new Array();
  licors = new Array();
  cuenta = new Array();
  $("#evento").val("evento");
  $("#importe").val(100);
  $("#personas").val(8);
  $("#impo").val(100);	
  cGo("pagemenu");
}

function Recalcule(ctx) {
	refreshimports();
	var contaper=0;
	for (i=0; i<350;i+=108) {
		for (j=0; j<420;j+=108) {
			if (contaper<personas) 
				{ //Personas
					cRect(ctx,true, 78 + j,114 + i,103,103,"s3",["s#333399","sblack"],4,80);
					cLink(ctx, page, strings, "rest", [78+j,114 + i,100,100], 'rest(ctx, opt,'+contaper+')', 'al_rest(ctx, opt,'+j+','+i+')');
					if (tipo==0 && menus[contaper]) {cRect(ctx,true, 78 + j,114 + i,105,105,"s2",["s#333399","sgrey"],4,80,[2,2,2,"#000"]);}
					if (tipo==1 && desserts[contaper]) {cRect(ctx,true, 78 + j,114 + i,105,105,"s2",["s#333399","syellow"],4,80,[2,2,2,"#000"]);}
					if (tipo==2 && cafes[contaper]) {cRect(ctx,true, 78 + j,114 + i,105,105,"s2",["s#333399","spurple"],4,80,[2,2,2,"#000"]);}
					if (tipo==3 && licors[contaper]) {cRect(ctx,true, 78 + j,114 + i,105,105,"s2",["s#333399","sgreen"],4,80,[2,2,2,"#000"]);}
					if (contaper==0) {nombre="Yo"} else {nombre=strings[1]+(contaper)};
					cText(ctx,true, nombre, 78 + j,84 + i, "f" , ["swhite","swhite"], 24, 100, [1,1,1,"#000"]);
					cText(ctx,true, cuenta[contaper] + "€", 78 + j,150 + i, "f" , ["sred","swhite"], 26, 100, [1,1,1,"#000"]);	
					if (menus[contaper]) {cRect(ctx,true, 42+j,114+i,22,22,"f",["sgrey","sgrey"],4,60,[1,1,1,"#000"]); cImg(ctx,true,_perso,42+j,114+i,100,[1,1,1,"#CCC"],[24,24]);}
					if (desserts[contaper]) {cRect(ctx,true, 67+j,114+i,22,22,"f",["syellow","syellow"],4,60,[1,1,1,"#000"]);cImg(ctx,true,_dessert,67+j,114+i,100,[1,1,1,"#CCC"],[24,24]);}
					if (cafes[contaper]) {cRect(ctx,true, 92+j,114+i,22,22,"f",["spurple","spurple"],4,60,[1,1,1,"#000"]);cImg(ctx,true,_cafe,92+j,114+i,100,[1,1,1,"#CCC"],[24,24]);}
					if (licors[contaper]) {cRect(ctx,true, 117+j,114+i,22,22,"f",["sgreen","sgreen"],4,60,[1,1,1,"#000"]);cImg(ctx,true,_licor,117+j,114+i,100,[1,1,1,"#CCC"],[24,24]);}
				} else { //OFF
					cRect(ctx,true, 78 + j,114 + i,103,103,"fs3",["sgrey","sgrey"],4,40,[2,2,2,"#000"]);
					if (contaper==parseInt(personas)) { cLink(ctx, page, strings, "plus", [78+j,114 + i,50,50], 'plus(ctx, opt,'+contaper+')', 'al_plus(ctx, opt,'+j+','+i+')');	}
				}
			contaper++;
		}
	}
}

function onChangeImport (e){
	comprovar = jQuery("#impo").val();
	
	if (comprovar<=0 || comprovar>9999) {compro=false; alert("Valor incorrecto");}

	

	precio_comida = importe_total;
	
	switch(tipo) {
	  	case 0: precio_comida = jQuery("#impo").val(); break;
	  	case 1: precio_postre = jQuery("#impo").val(); precio_comida = parseInt(precio_comida) - parseInt(precio_postre); break;
	  	case 2: precio_cafe = jQuery("#impo").val(); precio_comida = parseInt(precio_comida) - parseInt(precio_postre) - parseInt(precio_cafe); break;
	  	case 3: precio_licor = jQuery("#impo").val(); precio_comida = parseInt(precio_comida) - parseInt(precio_postre) - parseInt(precio_cafe) - parseInt(precio_licor); break;
	}
	
	if (precio_comida<0) {compro=false;}
	
	if (compro==false) {precio_comida = importe_total; } else {	
		importe_total = parseInt(precio_comida) + parseInt(precio_postre) + parseInt(precio_cafe) + parseInt(precio_licor);
	
		var cuenta_total=0;
		var personas_totales=0;
		for (i=0; i<personas;i++) {
			cuenta_total = parseInt(cuenta_total) + cuenta[i];
			if (persons[i]) {personas_totales++;}
		}		
		var morosidad = parseInt(importe_total) - parseInt(cuenta_total);
	
		for (i=0; i<personas;i++) {
			cuenta[i]= parseInt(cuenta[i]) + (morosidad/personas_totales).toFixed(2);
		}		
		cRefreshpage = cActivepage;
		}
}

function refreshimports() {
	perso_con_comida=0;
	perso_con_postre=0;
	perso_con_cafe=0;
	perso_con_licor=0;
	
	for (i=0; i<personas;i++) {
		if (persons[i] && menus[i]) {perso_con_comida++;}
		if (persons[i] && desserts[i]) {perso_con_postre++;}
		if (persons[i] && cafes[i]) {perso_con_cafe++;}
		if (persons[i] && licors[i]) {perso_con_licor++;}
		cuenta[i]=0;
	}
	
	for (i=0; i<personas;i++) {
		if (persons[i] && menus[i]) {cuenta[i] = (cuenta[i] + (precio_comida/perso_con_comida)); }
		if (persons[i] && desserts[i]) {cuenta[i] = (cuenta[i] + (precio_postre/perso_con_postre));}
		if (persons[i] && cafes[i]) {cuenta[i] = (cuenta[i] + (precio_cafe/perso_con_cafe));}
		if (persons[i] && licors[i]) {cuenta[i] = (cuenta[i] + (precio_licor/perso_con_licor));}
	}	
	for (i=0; i<personas;i++) {
		cuenta[i] = cuenta[i].toFixed(2);
	}		

}

function al_rest(ctx, opt, j, i) {
	cRect(ctx,opt[1], 78 + j,114 + i,103,103,"s3",["s#333399","sred"]);
	switch(tipo) {
  	case 0: cImg(ctx,opt[1],_perso,78+j,114+i,40,[1,1,1,"#CCC"],[42,42]);break;
  	case 1: cImg(ctx,opt[1],_dessert,78+j,114+i,40,[1,1,1,"#CCC"],[42,42]); break;
  	case 2: cImg(ctx,opt[1],_cafe,78+j,114+i,40,[1,1,1,"#CCC"],[42,42]);break;
  	case 3: cImg(ctx,opt[1],_licor,78+j,114+i,40,[1,1,1,"#CCC"],[42,42]); break;
	}
	
}

function rest(ctx, opt, rest) {
	switch(tipo) {
		case 0: if (menus[rest]) { menus[rest]= false;} else {menus[rest]= true;} break;
		case 1: if (desserts[rest]) { desserts[rest]= false;} else {desserts[rest]= true;} break; 
		case 2: if (cafes[rest]) { cafes[rest]= false;} else {cafes[rest]= true;} break;
		case 3: if (licors[rest]) { licors[rest]= false;} else {licors[rest]= true;} break;
	}
	cRefreshpage = cActivepage;
}


function plus(ctx, opt, plus) {
	persons[plus]= true;
	switch(tipo) {
		case 0: menus[plus]= true; break;
		case 1: desserts[plus]= true;break;
		case 2: cafes[plus]= true;break;
		case 3: licors[plus]= true;break;
	}
	personas++;
	cRefreshpage = cActivepage;
}

function mirar_tipo(ctx) {
	if (tipo==0) {cText(ctx, true, cGeneral[9],100,585,"f",["swhite",null],32,100,[1,1,1,"#CCC"]);}
	if (tipo==1) {cText(ctx, true, cGeneral[10],100,585,"f",["swhite",null],32,100,[1,1,1,"#CCC"]);}
	if (tipo==2) {cText(ctx, true, cGeneral[11],100,585,"f",["swhite",null],32,100,[1,1,1,"#CCC"]);}
	if (tipo==3) {cText(ctx, true, cGeneral[12],100,585,"f",["swhite",null],32,100,[1,1,1,"#CCC"]);}
}


function perso (ctx, opt) {
	cRect(ctx,opt[1],60,cy+210,55,34,"s2",[null,"sred"],4,100);	
}

function dessert (ctx, opt) {
	cRect(ctx,opt[1],125,cy+210,55,34,"s2",[null,"sred"],4,100);
}

function cafe (ctx, opt) {
	cRect(ctx,opt[1],190,cy+210,55,34,"s2",[null,"sred"],4,100);
}

function licor (ctx, opt) {
	cRect(ctx,opt[1],255,cy+210,55,34,"s2",[null,"sred"],4,100);
}

function perso_click (ctx, opt) {
  	$("#impo").val(parseInt(precio_comida));
	cRect(ctx,true,80,585,120,30,"f",["s#333399",null],0,100);
	cText(ctx, true, cGeneral[9],80,585,"fs",["sblack","s#333399"],32,100,[1,1,1,"#CCC"]);
	tipo = 0; 	
	cRefreshpage = cActivepage;
}

function dessert_click (ctx, opt) {
	$("#impo").val(parseInt(precio_postre));
 	cRect(ctx,true,80,585,120,30,"f",["s#333399",null],0,100);
	cText(ctx, true, cGeneral[10],80,585,"fs",["sblack","s#333399"],32,100,[1,1,1,"#CCC"]);
	tipo = 1; 
	cRefreshpage = cActivepage;
}

function cafe_click (ctx, opt) {
	$("#impo").val(parseInt(precio_cafe));
	cRect(ctx,true,80,585,120,30,"f",["s#333399",null],0,100);
	cText(ctx, true, cGeneral[11],80,585,"fs",["sblack","s#333399"],32,100,[1,1,1,"#CCC"]);
	tipo = 2; 
	cRefreshpage = cActivepage;
}

function licor_click (ctx, opt) {
	$("#impo").val(parseInt(precio_licor));
	cRect(ctx,true,80,585,120,30,"f",["s#333399",null],0,100);
	cText(ctx, true, cGeneral[12],80,585,"fs",["sblack","s#333399"],32,100,[1,1,1,"#CCC"]);
	tipo = 3; 
	cRefreshpage = cActivepage;
}

function al_plus(ctx, opt, j, i) {
  cText(ctx,opt[0], "+", 78 + j,114 + i, "f" , ["swhite","swhite"], 38, 100, [1,1,1,"#000"]);

  switch(tipo) {
  	case 0: cImg(ctx,opt[1],_perso,78+j,114+i,60,[1,1,1,"#CCC"],[42,42]);break;
  	case 1: cImg(ctx,opt[1],_dessert,78+j,114+i,60,[1,1,1,"#CCC"],[42,42]); break;
  	case 2: cImg(ctx,opt[1],_cafe,78+j,114+i,60,[1,1,1,"#CCC"],[42,42]);break;
  	case 3: cImg(ctx,opt[1],_licor,78+j,114+i,60,[1,1,1,"#CCC"],[42,42]); break;
  }
}


function al_list (ctx, opt) {
	cText(ctx,opt[0], strings[3],395,530,"f",["swhite",null],32,100,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[3],395,530,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}

function al_save (ctx, opt) {
	cText(ctx,opt[0], strings[2],395,585,"f",["swhite",null],32,100,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[2],395,585,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}


///****************************************************
/// LOADER OPCIONES 
///****************************************************
function loader_pageoptions(ctx, page, width, height,strings) {
	cx=width/2; cy=height/2;
	cClear(ctx, 0,0,width, height, "black", true);
	cRect(ctx,true, cx,cy-10,width-10,height-30,"fs4",["s#333399","s#333399"],8,100,[2,2,2,"#000"]);
	cImg(ctx,true, _titulo,cx,50,100,2);
	//Eslogan
	cText(ctx,true,  cGeneral[8], cx, 110, "f" , ["swhite","swhite"], 22,100,[1,1,1,"#CCC"]);	
	//Cuadro menu
	cRect(ctx,true, cx,cy-20,width-100,320,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);
	//Cuadro titulomenu
	cRect(ctx,true, cx,cy-140,360,60,"fs4",["sblack","s#333399"],8,60,[2,2,2,"#000"]);
	cText(ctx,true,  strings[0], cx, 180, "f" , ["swhite","swhite"], 42,80,[1,1,1,"#000"]);
	cText(ctx,true,  strings[2], cx-78, 280, "s" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);
	cText(ctx,true,  strings[3], cx-78, 360, "s" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);
	cToggle(ctx, page, cGeneral, "graficos", [cx+75,280,200,40], 'ot_graficos(ctx, opt)',graphic);		
	cToggle(ctx, page, cGeneral, "idioma", [cx+75,360,200,40], 'ot_language(ctx, opt)',cAbrv.indexOf(cLanguage));	
	cLink(ctx, page, strings, "volver", [cx,515,290,50], 'cGo("pagemenu")', 'ol_back(ctx, opt)');	
	//Footer
	cRect(ctx,true,cx,590,width-20,40,"f",["sblack",null],8);
	cText(ctx,true,  cGeneral[2], 70, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  cGeneral[1], cx, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cLink(ctx, page, cGeneral, "mail", [width-70,590,100,20], 'cGo("mailto://oriol.faura@gmail.com", 1)', 'ml_mail(ctx, opt, ' + width + ')');
}

function ot_language(ctx, opt) {
	obj= cToggleAction(vtoggles, "idioma", 0,cLangs.length-1);
	
	var lang = cLangs[obj];
	cText(ctx, true, lang,cx+75,360,"f",["swhite",null],30,100,[1,1,1,"#CCC"]);
	
    old_value= cLanguage;
    cLanguage = cAbrv[obj];

    if (old_value!=cLanguage) {	
    							cGeneral = new Array(); cGeneral =  cStrings (cDirectoryLang, cLanguage, "general");
       							cConfig[0] = cLanguage; cSaveConfig();
       							if (cConfig[1]==2) {cTRANSITION="fade";} else {cTRANSITION="none";}
    							$.mobile.defaultPageTransition = cTRANSITION;
    						  }
}

function ot_graficos(ctx, opt) {
	obj= cToggleAction(vtoggles, "graficos", 0,2);
	switch (obj) {
		case 0: cText(ctx, true, cGeneral[5],cx+75,280,"f",["swhite",null],30,100,[1,1,1,"#CCC"]); break;
		case 1: cText(ctx, true, cGeneral[6],cx+75,280,"f",["swhite",null],30,100,[1,1,1,"#CCC"]); break;
		case 2: cText(ctx, true, cGeneral[7],cx+75,280,"f",["swhite",null],30,100,[1,1,1,"#CCC"]); break;
	}
    old_value= cConfig[1]; 
    if (old_value != obj) {  cConfig[1] = obj; cSaveConfig(); }	
}

function ol_back (ctx, opt) {
	cText(ctx,opt[0], strings[1],cx,515,"f",["swhite",null],32,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[1],cx,515,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}

///****************************************************
/// LOADER SAVE 
///****************************************************
function loader_pagesave(ctx, page, width, height,strings) {
	cx=width/2; cy=height/2;
	cClear(ctx, 0,0,width, height, "black", true);
	cRect(ctx,true, cx,cy-10,width-10,height-30,"fs4",["s#333399","s#333399"],8,100,[2,2,2,"#000"]);
	cImg(ctx,true, _titulo,cx,50,100,2);
	//Eslogan
	cText(ctx,true,  cGeneral[8], cx, 110, "f" , ["swhite","swhite"], 22,100,[1,1,1,"#CCC"]);	
	//Cuadro menu
	cRect(ctx,true, cx,cy-20,width-100,320,"fs4",["s#333399","s#333399"],8,60,[2,2,2,"#000"]);
	//Cuadro titulomenu
	cRect(ctx,true, cx,cy-140,360,60,"fs4",["sblack","s#333399"],8,60,[2,2,2,"#000"]);
	cText(ctx,true,  strings[0], cx, 180, "f" , ["swhite","swhite"], 42,80,[1,1,1,"#000"]);
	cText(ctx,true,  strings[2], cx-78, 280, "s" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);
	cText(ctx,true,  strings[3], cx-78, 360, "s" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);
	cText(ctx,true,  importe_total+"€", cx+78, 280, "f" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);
	cText(ctx,true,  cuenta[0]+"€", cx+78, 360, "f" , ["swhite","swhite"], 34, 100, [2,2,2,"#000"]);	
	
	cRect(ctx,true, 105,418,45,36,"f",["swhite","s#333399"],4,100,[2,2,2,"#000"]);
	cImg(ctx,true, _pay,105,418,100);

	cLink(ctx, page, strings, "pagar", [cx+30,420,290,50], '', 'sl_check(ctx, opt)');	
	cLink(ctx, page, strings, "menu", [cx,515,290,50], 'reset()', 'sl_back(ctx, opt)');		
	//Footer
	cRect(ctx,true,cx,590,width-20,40,"f",["sblack",null],8);
	cText(ctx,true,  cGeneral[2], 70, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cText(ctx,true,  cGeneral[1], cx, 590, "f" , ["swhite","swhite"], "18px sans-serif");
	cLink(ctx, page, cGeneral, "mail", [width-70,590,100,20], 'cGo("mailto://oriol.faura@gmail.com", 1)', 'ml_mail(ctx, opt, ' + width + ')');
	
	
}

function sl_check (ctx, opt) {
	cText(ctx,opt[0], strings[4],cx+30,420,"f",["swhite",null],38,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[4],cx+30,420,"fs",["swhite","sred"],40,100,[1,1,1,"#CCC"]);
}


function sl_back (ctx, opt) {
	cText(ctx,opt[0], strings[1],cx,515,"f",["swhite",null],32,80,[1,1,1,"#CCC"]); 
	cText(ctx,opt[1], strings[1],cx,515,"fs",["swhite","sred"],34,100,[1,1,1,"#CCC"]);
}